package com.siwei.mes.service.materials.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.materials.MaterialsTestProjectDesc;
import com.siwei.mes.mapper.materials.MaterialsTestProjectDescMapper;
import com.siwei.mes.service.materials.MaterialsTestProjectDescService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验说明表(MaterialsTestProjectDesc)表服务实现类
 *
 * @author linzi
 * @since 2024-05-18 23:25:58
 */
@Service
public class MaterialsTestProjectDescServiceImpl implements MaterialsTestProjectDescService {
    @Resource
    private MaterialsTestProjectDescMapper materialsTestProjectDescMapper;

    @Override
    public Long create(MaterialsTestProjectDesc entity) {
        /**查询对应类型的试验项目是否已经配置了说明*/
        assertTrue(this.isExist(entity) > 0, String.format("该试验类型【%s】中试验项目【%s】已有说明信息", entity.getMaterialTypeName(), entity.getItemName()));
        entity.init(true);
        materialsTestProjectDescMapper.insertSelective(entity);
        return entity.getId();
    }

    /**
     * 查询对应类型的试验项目是否已经配置了说明
     *
     * @param entity
     */
    private int isExist(MaterialsTestProjectDesc entity) {
        MaterialsTestProjectDesc select = new MaterialsTestProjectDesc();
        select.setMaterialType(entity.getMaterialType());
        select.setItemCode(entity.getItemCode());
        select.setIsDel(0);
        return materialsTestProjectDescMapper.selectCount(entity);
    }

    @Override
    public void update(MaterialsTestProjectDesc entity) {
        MaterialsTestProjectDesc materialsTestProjectDesc = materialsTestProjectDescMapper.selectByPrimaryKey(entity.getId());
        assertTrue(materialsTestProjectDesc == null, "试验说明表信息不存在");
        if (!Objects.equals(entity.getMaterialType(), materialsTestProjectDesc.getMaterialType()) ||
                !entity.getItemCode().equals(materialsTestProjectDesc.getItemCode())) {
            /**查询对应类型的试验项目是否已经配置了说明*/
            assertTrue(this.isExist(entity) > 0, String.format("该试验类型【%s】中试验项目【%s】已有说明信息", entity.getMaterialTypeName(), entity.getItemName()));
        }
        entity.init(false);
        materialsTestProjectDescMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        MaterialsTestProjectDesc materialsTestProjectDesc = materialsTestProjectDescMapper.selectByPrimaryKey(id);
        assertTrue(materialsTestProjectDesc == null, "试验说明表信息不存在");
        materialsTestProjectDescMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<MaterialsTestProjectDesc> getPage(PageQuery<MaterialsTestProjectDesc, MaterialsTestProjectDesc> query) {
        MaterialsTestProjectDesc searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> materialsTestProjectDescMapper.getList(searchParam));
    }

    @Override
    public MaterialsTestProjectDesc loadById(Long id) {
        return materialsTestProjectDescMapper.loadById(id);
    }

    @Override
    public List<MaterialsTestProjectDesc> getAll(MaterialsTestProjectDesc entity) {
        return materialsTestProjectDescMapper.getList(entity);
    }

    @Override
    public MaterialsTestProjectDesc loadByItemCode(String itemCode) {
        return materialsTestProjectDescMapper.loadByItemCode(itemCode);
    }
}
